package com.fmboy.MXH.nav.controller;

import com.fmboy.MXH.nav.domain.AdminRequired;
import com.fmboy.MXH.nav.domain.ErrorCode;
import com.fmboy.MXH.nav.domain.Result.Result;
import com.fmboy.MXH.nav.domain.dto.CategoryDTO;
import com.fmboy.MXH.nav.domain.pojo.CategoryVO;
import com.fmboy.MXH.nav.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@AdminRequired
@RestController
@Tag(name = "管理员分类接口", description = "adminCategory API")
@RequestMapping("/mxh-nav/admin/category")
public class AdminCategoryController {
    @Resource
    private CategoryService categoryService;

    @GetMapping
    @Operation(summary = "获取-分类", description = "获取-分类")
    public Result<List<CategoryVO>> getCategories(
            @RequestParam(required = false) String category_name,
            @RequestParam(required = false) String state) {

        CategoryDTO categoryDTO = new CategoryDTO();
        categoryDTO.setCategory_name(category_name);
        categoryDTO.setState(state);

        List<CategoryVO> categories = categoryService.AdminListByUserId(categoryDTO);
        if (categories.isEmpty()) {
            return Result.error(ErrorCode.UNAUTHORIZED, "获取失败");
        }

        return Result.success("获取分类成功", categories);
    }

    //获取-分类详情
    @GetMapping("/info")
    @Operation(summary = "获取-分类详情", description = "获取-分类详情")
    public Result<CategoryVO> getCategoryDetail(@RequestParam Integer id) {

        CategoryVO categoryVO = categoryService.ByUserId(id);
        if (categoryVO == null) {
            return Result.error(ErrorCode.UNAUTHORIZED, "获取失败");
        }

        return Result.success("获取分类详情成功", categoryVO);
    }

    //修改-分类
    @Operation(summary = "修改-分类", description = "修改-分类")
    @PutMapping
    public Result<String> updateCategory(@RequestBody CategoryDTO categoryDTO) {
        try {
            categoryService.AdminUpdateCategory(categoryDTO);
            return Result.success("修改分类成功");
        } catch (Exception e) {
            return Result.error(ErrorCode.UNAUTHORIZED, e.getMessage());
        }
    }

    //修改-分类发布状态
    @PutMapping("/state")
    @Operation(summary = "修改-分类发布状态", description = "修改-分类发布状态")
    public Result<String> updateCategoryState(@RequestBody CategoryDTO categoryDTO) {
        try {
            categoryService.AdminUpdateCategoryState(categoryDTO);
            return Result.success("修改成功！");
        } catch (Exception e) {
            return Result.error(ErrorCode.UNAUTHORIZED, e.getMessage());
        }
    }

    //删除-分类
    @DeleteMapping
    @Operation(summary = "删除-分类", description = "删除-分类")
    public Result<String> deleteCategory(@RequestParam Integer id) {
        try {
            categoryService.AdminUserById(id);
            return Result.success("删除成功！");
        } catch (Exception e) {
            return Result.error(ErrorCode.UNAUTHORIZED, e.getMessage());
        }
    }

}
